import numpy as np
import pandas as pd
from scipy.optimize import brute
from functools import partial
from pandas import DataFrame,Series
import time
import os
import sys


file_path = "data/english_records_0613.csv"
log = "data/log"
train = "data/train.csv"

log_w=open(log,"w")
knowledge_key = []

def read_data(file):
	df = pd.read_csv(file)
	return df

def reduce_knowledge(data):
    knowledge = data["knowledge_point"].values
    global knowledge_key
    def get_pos(x):
    	return str(x)[0:2]
    knowledge_point = map(get_pos,knowledge)
    reduce_k = (list(knowledge_point))
    k = Series(reduce_k)
    knowledge_key = k.unique()
    log_w.write("reduce knowledge\n")
    log_w.write(",".join(knowledge_key)+"\n")
    data.insert(0,"kc_reduce",reduce_k)
    data = data.loc[:,['kc_reduce','user_id','difficulty','correct']]
    return data


def compute_lo():
    data = read_data(file_path)
    global knowledge_key
    cluster = data["user_id"].unique()
    data = reduce_knowledge(data)
    data.to_csv(train)
    correct = []
    for k in knowledge_key:
    	for i in cluster:
        	s2=data[(data['user_id']==i) & (data["kc_reduce"]==k)]
        	if len(s2) > 0:
        		correct.append(s2.correct.values[0])
    	log_w.write(str(k)+" L0:")
    	log_w.write(str(np.array(correct).mean())+"\n")
    log_w.write("finish reduce and compute init\n")
compute_lo()


# def split_2_train_test(df):
# 	df = read_data("english.csv")
# 	cluster = df.groupby('s_id').size()
# 	size =  (len(cluster.values))
# 	test_count = int(size/10)
# 	index = 0
# 	test_sid = []
# 	for name, group in df.groupby('s_id'):
# 		if (index > test_count):break
# 		test_sid.append(name)
# 		index = index + 1
# 	return test_sid


# test_uid = split_2_train_test(df)

# row_test = []
# row_train = []
# for idx, values in df.iterrows():
# 	uid =  (values["s_id"])
# 	if uid in test_uid:
# 		row_test.append(values.values[1:])
# 	else:
# 		row_train.append(values.values[1:])

# df = DataFrame(row_test)
# df = df.rename(columns={0: 's_id', 1: 'sub_time', 2: 'prob_id', 3: 'kc', 4: 'correct', 5: 'difficulty', 6: 'kc_reduce'})
# df.to_csv(test_file_path)

# df = DataFrame(row_train)
# df = df.rename(columns={0: 's_id', 1: 'sub_time', 2: 'prob_id', 3: 'kc', 4: 'correct', 5: 'difficulty', 6: 'kc_reduce'})
# df.to_csv(train_file_path)
# print ("ok")
